//
//  HLIMKitTextMessageContentConfig.m
//  YueYue
//
//  Created by hulu001 on 2024/1/12.
//

#import "HLIMKitTextMessageContentConfig.h"
#import "HLChatKit.h"
#import "M80AttributedLabel+HLIMKit.h"

@interface HLIMKitTextMessageContentConfig ()

@property (strong, nonatomic) M80AttributedLabel *contentLabel;

@end

@implementation HLIMKitTextMessageContentConfig


/// 计算气泡尺寸
/// @param cellWidth cell的宽
/// @param message 消息
- (CGSize)contentSize:(CGFloat)cellWidth message:(NIMMessage *)message
{
    NSString *text = nil;
    
    if (message.isOutgoingMsg) {
        /*
         自己的消息
         规则：如果本地扩展字段有自己输入的原文，直接显示原文。
         */
        text = message.localExt[@"originContent"];
        if (text) {
            [self.contentLabel im_setText:text];
        } else {
            [self.contentLabel im_setText:message.text];
        }
    } else {
        [self.contentLabel im_setText:message.text];
    }
    
    
    HLIMKitMessageSetting *setting = [HLChatKit.shareSDK.config setting:message];
    self.contentLabel.font = setting.font;
    
    CGFloat msgBubbleMaxWidth    = setting.bubbleMaxWidth;
    CGFloat bubbleLeftToContent  = setting.contentInsets.left;
    CGFloat contentRightToBubble = setting.contentInsets.right;
    CGFloat msgContentMaxWidth = (msgBubbleMaxWidth - contentRightToBubble - bubbleLeftToContent);
    
    // 气泡默认内容的尺寸
    CGSize originTextSize = [self.contentLabel sizeThatFits:CGSizeMake(msgContentMaxWidth, CGFLOAT_MAX)];
    CGSize size = originTextSize;
    
    
    NSString *trans = message.localExt[@"trans"];
    NSString *pin = message.localExt[@"pin"];
    NSString *autoExpandFlag = message.localExt[@"autoExpandFlag"]; // 收到消息是否需要默认展开的标记
    
    NSMutableDictionary *localExt = nil;
    
    if (message.localExt) {
        localExt = [NSMutableDictionary dictionaryWithDictionary:message.localExt];
    } else {
        localExt = [NSMutableDictionary dictionary];
    }
    
    if ([message.remoteExt[@"extType"] isEqualToString:@"chatUpMsgExt"]) {
        // 搭讪消息
        if (autoExpandFlag.length == 0) {
            // 第一次显示消息，翻译内容来自消息扩展，并且标记已显示过
            if (message.remoteExt[@"ext"]) {
                trans = message.remoteExt[@"ext"];
                localExt[@"trans"] = trans;
                localExt[@"autoExpandFlag"] = @"1";
            }
        } else {
            // 不是第一次显示消息
            
        }
    } else {
        
    }
    
    localExt[@"originTextHeight"] = @(originTextSize.height);
    
    if (pin.length) {
        CGSize pinSize = [pin boundingRectWithSize:CGSizeMake(msgContentMaxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:setting.font} context:nil].size;
        size.height = size.height + 3 + pinSize.height;
        
        localExt[@"pinSizeHeight"] = @(pinSize.height);
        size.width = MAX(size.width, pinSize.width);
    }
    
    if (trans.length) {
        
        CGSize transSize = [trans boundingRectWithSize:CGSizeMake(msgContentMaxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:setting.font} context:nil].size;
        size.height = size.height + 17 + transSize.height;
        size.width = MAX(size.width, transSize.width);
        localExt[@"transSizeHeight"] = @(transSize.height);
    }
    
    // 更新本地扩展
    message.localExt = localExt;
    
    return size;
}

/// content view 的名字（一般是类名）
/// @param message 消息
- (NSString *)cellContent:(NIMMessage *)message
{
    return @"HLIMKitMessageTextContentView";
}

/// cell内容距离气泡的内间距
/// @param message 消息
- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message;
{
    return [[HLChatKit.shareSDK config] setting:message].contentInsets;
}

- (UIEdgeInsets)cellInsets:(NIMMessage *)message
{
    // 要不要显示昵称 气泡位置分别设置
    if ([[HLChatKit.shareSDK.config setting:message] showAvatar])
    {
        id typeObject = message.remoteExt[@"type"];
        NSInteger type = 0;
        if ([typeObject isKindOfClass:NSNumber.class] || [typeObject isKindOfClass:NSString.class]) {
            type = [typeObject integerValue];
        }
        
        if (type == 20 || type == 21) {
            return UIEdgeInsetsMake(4, 54, 32, 56);
        }
        return UIEdgeInsetsMake(4, 54, 16, 56);
    }
    else
    {
        return UIEdgeInsetsMake(4, 54, 16, 12);
    }
}

#pragma mark - contentLabel

- (M80AttributedLabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
        _contentLabel.backgroundColor = UIColor.clearColor;
        _contentLabel.font = [UIFont systemFontOfSize:16.f];
    }
    return _contentLabel;
}

@end
